#import "GameEngineiPhone.h"


UIApplication* m_pUIApp;

@implementation GameEngineiPhone
- (void) applicationDidFinishLaunching:(UIApplication*)application 
{
		m_pUIApp = application;
		
		CGRect	rect = [[UIScreen mainScreen] bounds];
	
		//Create a full-screen window
		_window = [[UIWindow alloc] initWithFrame:rect];
	
#ifndef USE_DEPTH_BUFFER_IPHONE
		_mainView = [[EAGLView alloc] initWithFrame: rect];
#else
		_mainView = [[EAGLView alloc] initWithFrame: rect pixelFormat:kEAGLColorFormatRGBA8 depthFormat:GL_DEPTH_COMPONENT24_OES preserveBackbuffer:NO];
#endif
		[_window addSubview:_mainView];
		
		[self initialize];
	

		timer = [NSTimer 
	        scheduledTimerWithTimeInterval:(1.0f/30.0f)
	        target: self 
			selector: @selector(loop)
	        userInfo: nil
	        repeats: YES ];

		[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0f/60.0f)];
		[[UIAccelerometer sharedAccelerometer] setDelegate:self];

		//Show the window
		[_window makeKeyAndVisible];
	
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
	
		
	if(g_gameApp)
	{
		g_gameApp->showNotify();
	}

	printf("end ignoring events");
	[m_pUIApp endIgnoringInteractionEvents];

}

- (void)applicationWillResignActive:(UIApplication *)application
{

	if(g_gameApp)
	{
		g_gameApp->hideNotify();
	}

	printf("ignoring events");
	[m_pUIApp beginIgnoringInteractionEvents];
}

- (void)applicationWillTerminate: (UIApplication *)application
{
	printf("terminate app");
	[timer invalidate];
	[timer release];
	
	if(g_gameApp)
	{
		delete g_gameApp;
		g_gameApp = NULL;
	}	
	
	
	[_mainView release];
	[_window release];
}
- (void) dealloc 
{
	[self release];
	[super dealloc];	
}


- (void) loop 
{
	//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	glShadeModel(GL_SMOOTH);
	g_gameApp->paint();
	g_gameApp->update();
	::glFlush();
	//::glFinish();
	[_mainView swapBuffers];
}


- (void) initialize 
{
	char workingDir [1024];
	char rmsPath [1024];
		
	sprintf(workingDir, "%s/",[[[NSBundle mainBundle] resourcePath] UTF8String]);
	chdir(workingDir);
	printf("Path:\n%s\n\n",getcwd(NULL, 0));


	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString * rmsDir = [paths objectAtIndex:0];
	sprintf(rmsPath, "%s", [rmsDir UTF8String]);
	printf("DirPath:\n%s\n\n",rmsPath);

	//To know the current language
	NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
	NSArray* languages = [defs objectForKey:@"AppleLanguages"];
	NSString* preferredLang = [languages objectAtIndex:0];
	//char lang[255];
	//sprintf(Game::m_DeviceLanguage, "%s", [preferredLang UTF8String]);

	g_gameApp = new Game();

}


- (void) accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
	/*
	if(acceleration.x > 0.04 || acceleration.x < -0.04)
		g_gameApp->m_angle_x = 4 * acceleration.x;
	else
		g_gameApp->m_angle_x = 0;
		
	if(acceleration.y > 0.04 || acceleration.y < -0.04)
		g_gameApp->m_angle_y = 4 * acceleration.y;
	 */
}

@end

